package com.example.administrator.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;

import android.widget.Toast;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LogoActivity extends AppCompatActivity {
    private static  String url_content="http://192.168.1.106:8888/SpringWebMvcTest/selectTable?";
    private Button tijiao;
    private EditText yuanxiao,project,name,No;
    private String yuanxiaoValue,projectValue,nameValue,NoValue;
    //private List<Table> tables;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logo);
        initView();
    }
    public void initView(){
        //输入第一个字符自动出现相应的名字
        //院校
        List<String> data1=new ArrayList<>();
        data1.add("内蒙古民族大学");data1.add("吉林师范大学");
        AutoCompleteTextView yuanxiaoTv=(AutoCompleteTextView)findViewById(R.id.tv_yuanxiao);
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data1);
        yuanxiaoTv.setAdapter(adapter1);

//        //教师姓名
//        final  List<Teacher> data=new ArrayList<Teacher>();
//        Teacher t1=new Teacher("001","john");
//        Teacher t2=new Teacher("002","tom");
//        data.add(t1);data.add(t2);
//
//        AutoCompleteTextView teachersTv=(AutoCompleteTextView)findViewById(R.id.tv_name);
//        ArrayAdapter<Teacher> adapter=new ArrayAdapter<Teacher>(this,android.R.layout.simple_list_item_1,data);
//        teachersTv.setAdapter(adapter);
//
//        teachersTv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Teacher t=data.get(position);
//                Toast.makeText(LogoActivity.this,t.getId(),Toast.LENGTH_SHORT).show();
//            }
//        });

        //工号
        List<String> data4=new ArrayList<>();
        data4.add("3113070");data4.add("3116371");data4.add("3116372");
        AutoCompleteTextView NoTv=(AutoCompleteTextView)findViewById(R.id.tv_No);
        ArrayAdapter<String> adapter4=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data4);
        NoTv.setAdapter(adapter4);

        yuanxiao=(EditText)findViewById(R.id.tv_yuanxiao);
        //name=(EditText)findViewById(R.id.tv_name);
        No=(EditText)findViewById(R.id.tv_No);
        Button tijiao=(Button)findViewById(R.id.tijiao);
        tijiao.getBackground().setAlpha(100);
    }


    public void submit(View view) throws Exception{
        yuanxiaoValue=yuanxiao.getText().toString();
//        projectValue=project.getText().toString();
       // nameValue=name.getText().toString();
        NoValue=No.getText().toString();

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    selectByPost(yuanxiaoValue,NoValue);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
//String school,String tid
    public void selectByPost(String school,String id) throws Exception{
//        JSONObject json = new JSONObject();
//        try {
//            json.put("学校",yuanxiaoValue);
//            json.put("工号",NoValue);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        OkHttpClient client = new OkHttpClient();
        String url = url_content+"school="+school+"&id="+id;
        //final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
//        RequestBody body = RequestBody.create(JSON, String.valueOf(json));
//                    body = new FormEncodingBuilder()
//                            .build();
        Request request = new Request.Builder()
                .url(url)
                //.post(body)
                .build();
        Log.i("kun",url);

        Response response = client.newCall(request).execute();
        Log.i("isSuccess",response.isSuccessful()+"");
        if (response.isSuccessful()){
            String list = response.body().string();
            Log.i("kun2",list);
            //JsonParse jsonParse = new JsonParse();
            JsonParse.sonParse.getListTable(list);
        }else{

        }
    }
}
